<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增量化收益')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-profit-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input id="profitTime" name="profitTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">用户：</label>
                <div class="col-sm-8">
<!--                    <input name="userName" class="form-control" type="text">-->
                    <select id="loginName" name="loginName" class="form-control">
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">收益值：</label>
                <div class="col-sm-8">
                    <input id="profitValue" name="profitValue" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: select2-js" />

    <script th:inline="javascript">
        var prefix = ctx + "quantify/profit"
        $("#form-profit-add").validate({
            focusCleanup: true,
            rules:{
                profitTime:"required",
                loginName: "required",
                profitValue:{
                    required:true,
                    number:true
                },
            },
            message:{
                profitTime: "请选择日期",
                loginName: "请选择用户",
                profitValue:{
                    required:"请填写数值",
                    number:"只允许数字"
                },
            }
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data=$('#form-profit-add').serializeArray();
                //var  val= $('#loginName').val();
                var  text=$('#loginName option:selected').text();
                //data.push({"name": "loginName", "value": val});
                num=text.lastIndexOf("(")
                text=num>0?text.slice(0,num):text;
                data.push({"name": "userName", "value": text});
                $.operate.save(prefix + "/add", data);
            }
        }

        $("input[name='profitTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });


        // $(function() {
        //     $('#loginName').select2({
        //         placeholder: "请选择用户",
        //         allowClear: true
        //     });
        // })

        $(function() {
            $('#loginName').select2({
                placeholder: "请选择用户",
                ajax: {
                    url: prefix+"/user/list",
                    type:'POST',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            userName: params.term,
                        };
                    },
                    processResults: function (data) {
                        var itemList = [];//当数据对象不是{id:0,text:'ANTS'}这种形式的时候，可以使用类似此方法创建新的数组对象
                        var list = data.rows
                        $.each(list,function(index,items){
                            console.info(index+":"+items);
                            itemList.push({id: items.loginName, text: items.userName+'('+items.loginName+')'});

                        });
                        return {
                            results: itemList
                        };
                    },
                    cache: true
                }
        })})


    </script>
</body>
</html>